package com.file.system.service.impl.system;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.file.system.entity.system.WebLog;
import com.file.system.entity.res.DataPage;
import com.file.system.mapper.system.WebLogMapper;
import com.file.system.service.system.IWebLogService;
import com.file.system.tools.page.DP;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 日志 服务实现类
 *
 * @author 靳春晖
 * @since 2020-05-13
 */
@Service
@Transactional(rollbackFor = Exception.class)
@DS("weblog")
public class WebLogServiceImpl implements IWebLogService {

    @Autowired
    private WebLogMapper mapper;

    //添加日志
    public boolean save(WebLog weblog) {
        return SqlHelper.retBool(mapper.save(weblog));
    }


    //删除日志
    public boolean remove(@Param("ew") UpdateWrapper<WebLog> query) {
        return SqlHelper.retBool(mapper.remove(query));
    }

    //编辑日志
    public boolean update(@Param("et") WebLog weblog, @Param("ew") UpdateWrapper<WebLog> query) {
        return SqlHelper.retBool(mapper.update(weblog, query));
    }

    //添加日志(批量)
    public boolean saveBatch(@Param("list") List<WebLog> weblogs) {
        if (weblogs.size() < 1) {
            return false;
        }
        return SqlHelper.retBool(mapper.saveBatch(weblogs));
    }


    //查询字典分页
    @DS("slave")
    public DataPage page(@Param("pageIndex") Integer pageIndex, @Param("pageSize") Integer pageSize, @Param("ew") QueryWrapper<WebLog> query) {
        //查询数量
        int count = mapper.selectCount(query);
        DP dp = DP.init().paging(pageIndex, pageSize, count);
        Page<WebLog> page = new Page<>(dp.getPid(), dp.getPageSize());
        page.setSearchCount(false);
        List<WebLog> list = mapper.select(page, query);
        return DataPage.init().set(count, list);
    }

    //查询日志
    @DS("slave")
    public List<WebLog> list(@Param("ew") QueryWrapper<WebLog> query) {
        return mapper.select(query);
    }

    //查询日志分页
    @DS("slave")
    public List<WebLog> page(Page<WebLog> page, @Param("ew") QueryWrapper<WebLog> query) {
        page.setSearchCount(false);
        return mapper.select(page, query);
    }

    //通过id查询日志
    @DS("slave")
    public WebLog getById(@Param("id") Long id) {
        return mapper.selectById(id);
    }

    //查询日志数量
    @DS("slave")
    public int count(@Param("ew") QueryWrapper<WebLog> query) {
        return mapper.selectCount(query);
    }
}
